(ns qwiz.db.Db)

(use 'korma.db)

(defdb prod (postgres {:db "qwiz"
                       :user "postgres"
                       :password "password"}))

(use 'korma.core)


(defentity questions)

(defn initDb [data]
  (delete questions)
  (insert questions
    (values data)))

(def questions-count ((first (select questions  (aggregate (count :*) :cnt))) :cnt))


(defn get-rand-question []
  (first (select questions (limit 1) (offset (Math/round (rand questions-count))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;; tests ;;;;;;;;;;;;;;;;;;;;
;(println (get-rand-question))